package org.accp.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.accp.dao.*;
import org.accp.pojo.*;
import org.accp.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
@CrossOrigin("*")
public class CustomerController {
    @Autowired
    private CustomerDao customerDao;
    @RequestMapping("/search")
    @ResponseBody
    public List<Customer> search() throws Exception{
        return customerDao.getAll();
    }
    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Customer>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Customer condition) throws Exception {
        Map<String,Object> map = new HashMap<>();
        Page<Customer> pager = PageHelper.startPage(page, limit);
        List<Customer> list = customerDao.getConditionList(condition);
        return new ResponseResult(pager.getTotal(),list);
    }
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(Integer cid) throws Exception{
        customerDao.delete(cid);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Customer customer) throws Exception{
        customerDao.add(customer);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(Customer customer) throws Exception{
        customerDao.update(customer);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/updates")
    @ResponseBody
    public ResponseResult updates(Customer customer) throws Exception{
        customerDao.updates(customer);
        return ResponseResult.createSuccessResult();
    }

}
